import sys
import ctypes
#sys.path.append("../")
sys.path.append("./")
import json
from ctypes import *
import time
import queue
from bcos3sdk.bcos3sdk_wrap import NativeBcos3sdk, BCOS_CALLBACK_FUNC, BcosReqContext, BcosCallbackFuture, \
    BCOS_AMOP_SUB_CALLBACK_FUNC

##---------------------------------------------------------
##以下为测试代码
#应用层回调



group_id = "group".encode("utf-8")


bcossdk = NativeBcos3sdk("./bcos3sdklib/bcos3config.ini")
print("sdkobj:",bcossdk)
cbfuture = BcosCallbackFuture();
cb_func = BCOS_CALLBACK_FUNC(cbfuture.bcos_callback)
n = 100

key_pair = bcossdk.bcos_sdk_create_keypair(1) # 1:ecdsa  2:sm

address = bcossdk.bcos_sdk_get_keypair_address(key_pair)
print(" ==> new key pair, address: {}".format(address))

pub = bcossdk.bcos_sdk_get_keypair_public_key(key_pair)
print(" ==> pub: {}".format(address))

pri = bcossdk.bcos_sdk_get_keypair_private_key(key_pair)
print(" ==> pri: {}".format(pri))

chain_id = bcossdk.bcos_sdk_get_group_chain_id(bcossdk.sdk, group_id)
print(" ==> chain_id: {}".format(chain_id))

wasm = ctypes.c_int()
sm_crypto = ctypes.c_int()

bcossdk.bcos_sdk_get_group_wasm_and_crypto(bcossdk.sdk, group_id, byref(wasm), byref(sm_crypto))
print(" ==> wasm: {}, sm_crypto: {}".format(wasm, sm_crypto))

cb_context = BcosReqContext(n, "bcos_rpc_get_block_by_number", "this is test 1332")
bcossdk.bcos_rpc_get_block_by_number(bcossdk.sdk, group_id, None, 1, 1, 1, cb_func, byref(cb_context))
cbfuture.wait()
cbfuture.display()
n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_group_node_info", "this is test " + str(n))
bcossdk.bcos_rpc_get_group_node_info(bcossdk.sdk, group_id,b"", cb_func, byref(cb_context))
cbfuture.wait()
cbfuture.display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_group_info_list", "this is test " + str(n))
bcossdk.bcos_rpc_get_group_info_list(bcossdk.sdk, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_group_info", "this is test " + str(n))
bcossdk.bcos_rpc_get_group_info(bcossdk.sdk, group_id, cb_func, byref(cb_context))
resp = cbfuture.wait(2).display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_group_list", "this is test " + str(n))
bcossdk.bcos_rpc_get_group_list(bcossdk.sdk, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_peers", "this is test " + str(n))
bcossdk.bcos_rpc_get_peers(bcossdk.sdk, cb_func, byref(cb_context))
cbfuture.wait().display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_group_peers", "this is test " + str(n))
bcossdk.bcos_rpc_get_group_peers(bcossdk.sdk, group_id, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_total_transaction_count", "this is test " + str(n))
bcossdk.bcos_rpc_get_total_transaction_count(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_system_config_by_key", "this is test " + str(n))
bcossdk.bcos_rpc_get_system_config_by_key(bcossdk.sdk, group_id, None,b"key", cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_consensus_status", "this is test " + str(n))
bcossdk.bcos_rpc_get_consensus_status(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_sync_status", "this is test " + str(n))
bcossdk.bcos_rpc_get_sync_status(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_pending_tx_size", "this is test " + str(n))
bcossdk.bcos_rpc_get_pending_tx_size(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_pbft_view", "this is test " + str(n))
bcossdk.bcos_rpc_get_pbft_view(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_observer_list", "this is test " + str(n))
bcossdk.bcos_rpc_get_observer_list(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_sealer_list", "this is test " + str(n))
bcossdk.bcos_rpc_get_sealer_list(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_code", "this is test " + str(n))
bcossdk.bcos_rpc_get_code(bcossdk.sdk, group_id, None,b"0x1001", cb_func, byref(cb_context))
cbfuture.wait().display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_block_number", "this is test " + str(n))
bcossdk.bcos_rpc_get_block_number(bcossdk.sdk, group_id, None, cb_func, byref(cb_context))
cbfuture.wait().display()

n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_block_limit", "this is test " + str(n))
l = bcossdk.bcos_rpc_get_block_limit(bcossdk.sdk,group_id)
print("blocklimit ",l)





n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_block_hash_by_number", "this is test " + str(n))
bcossdk.bcos_rpc_get_block_hash_by_number(bcossdk.sdk, group_id, None, 1, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_block_by_hash", "this is test " + str(n))
bh = b"0xb92f29d369161c8306342d41f6702d9ce12f0a8898d05b83b08f03b7334f1052"
bcossdk.bcos_rpc_get_block_by_hash(bcossdk.sdk, group_id, None, bh, 0, 0, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_transaction_receipt", "this is test " + str(n))
th = b"0xc0a367cb5d11f21fd51196e9683cbfc2b8cd33c2e86c559d67142152f5fa7ee5"
bcossdk.bcos_rpc_get_transaction_receipt(bcossdk.sdk, group_id, None, th, 0, cb_func, byref(cb_context))
cbfuture.wait().display()


n=n+1
cb_context = BcosReqContext(n, "bcos_rpc_get_transaction", "this is test " + str(n))
th = b"0xc0a367cb5d11f21fd51196e9683cbfc2b8cd33c2e86c559d67142152f5fa7ee5"
bcossdk.bcos_rpc_get_transaction(bcossdk.sdk, group_id, None, th, 0, cb_func, byref(cb_context))
cbfuture.wait().display()

n=n+1
cb_context = BcosReqContext(n, "bcos_event_sub_subscribe_event", "this is test " + str(n))
l = bcossdk.bcos_event_sub_subscribe_event(bcossdk.sdk, group_id,b'param', cb_func, byref(cb_context))
print("eventsub ",l)
cbfuture.wait().display()

bcossdk.bcos_event_sub_unsubscribe_event(bcossdk.sdk,b'param')

n=n+1
cb_context = BcosReqContext(n, "bcos_amop_subscribe_topic", "this is test " + str(n))

TOPIC_ARRAY_3 = c_char_p *3
topics = TOPIC_ARRAY_3()
topics[0] = b"chongqing001"
topics[1] = b"chengdu002"
topics[2] = b"guiyang003"
bcossdk.bcos_amop_subscribe_topic(bcossdk.sdk,topics,3)
print(">>>>bcos_amop_subscribe_topic: done");


def bcos_sdk_c_amop_subscribe_cb(endpoint, seq,  resp):
    print("bcos_sdk_c_amop_subscribe_cb callback")
    epstr= str(endpoint, "utf-8")
    seqstr = str(seq,"utf-8")
    print("bcos_sdk_c_amop_subscribe_cb ",epstr,seqstr)
    if resp is None:
        print("resp is None")
    else:
        print("resp is ",resp)

n=n+1
cb_context = BcosReqContext(n, "bcos_amop_subscribe_topic_with_cb", "this is test " + str(n))

topic = b'abcdefg'
print("try bcos_amop_subscribe_topic_with_cb")
bcossdk.bcos_amop_subscribe_topic_with_cb(bcossdk.sdk,topic,BCOS_AMOP_SUB_CALLBACK_FUNC(bcos_sdk_c_amop_subscribe_cb),byref(cb_context))


#----------------------------------------

time.sleep(2)
bcossdk.bcos_sdk_stop(bcossdk.sdk)
bcossdk.bcos_sdk_destroy(bcossdk.sdk)